package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallCustomer;
import com.fau.cm.entity.MedicalCallUntreated;
import com.fau.cm.util.FileNameEncoderUtil;
import com.fau.cm.util.SystemConfig;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  我的话务管理未处理任务Action
 * @author yangzj
 * @datetime 2014年10月14日,上午10:39:10
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallManageMyUntAction extends PublicAction implements ModelDriven<MedicalCallUntreated>{
	
	private static final long serialVersionUID = 6349279192012513543L;
	
	private MedicalCallUntreated callUnt = new MedicalCallUntreated();
	
	/**
	 * @desc 页面初始化加载Tabs
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:03:41
	 */
	@Action(value = "call_manage_my_init", results = {@Result(location = "call_manage_my_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 未处理话务任务列表页
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月24日,下午10:45:23
	 */
	@Action(value = "call_manage_my_unt_init", results = {@Result(location = "call_manage_my_unt_init.jsp")})
	public String untreated(){
		return SUCCESS;
	}

	/**
	 * @desc 分页查询话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月15日,下午4:46:43
	 */
	@Action(value = "call_manage_my_unt_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getCallMyManagerService().findCallUntreatedByPage(callUnt, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改类别
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月23日,下午2:07:46
	 */
	@Action(value = "call_manage_my_unt_cat")
	public String editCategory(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callUnt.getId()) && StringUtils.isNotBlank(callUnt.getCategory())){
			this.baseAllService.getCallMyManagerService().editCallUntreatedCategory(callUnt.getId(), callUnt.getCategory());
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量修改任务状态
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月25日,下午4:59:10
	 */
	@Action(value = "call_manage_my_unt_sta")
	public String editStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callUnt.getId()) && StringUtils.isNotBlank(callUnt.getPocessingStatus())) {
			this.baseAllService.getCallMyManagerService().editCallUntreatedSta(callUnt.getId(), callUnt.getPocessingStatus(), callUnt.getGroupId(), callUnt.getHandleUserId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前人所在中心的所有话务组
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月14日,下午5:18:16
	 */
	@Action(value = "call_manage_my_unt_group")
	public String groupView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			list = this.baseAllService.getCallManagerService().findGroupList(admin.getGroup().getPlatform().getId());
			formatJSON(response, list);
		}
		return null;
	}
	
	/**
	 * @desc 根据组查话务员
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月14日,下午5:21:11
	 */
	@Action(value = "call_manage_my_unt_seat")
	public String seatView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getCallManagerService().findUserList(callUnt.getGroupId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 删除话务任务
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月19日,下午7:25:29
	 */
	@Action(value = "call_manage_my_unt_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(callUnt.getId())){
			this.baseAllService.getCallMyManagerService().delUntreatedCall(callUnt);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 跳转到任务处理tabs页面
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月31日,上午11:22:43
	 */
	@Action(value = "call_manage_my_unt_open", results = {@Result(location = "call_manage_my_unt_open.jsp")})
	public String openTask() {
		return SUCCESS;
	}
	
	/**
	 * @desc 转到客户信息页面tab
	 * @return
	 * @author kangyj
	 * @datetime 2014年10月31日,上午11:44:47
	 */
	@Action(value = "call_manage_my_unt_customer", results = {@Result(location = "call_manage_my_unt_customer.jsp")})
	public String customer() {
		return SUCCESS;
	}
	
	/**
	 * @desc 通过话务id查询用户信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月6日,下午5:33:49
	 */
	@Action(value = "call_manage_my_unt_find_customer")
	public String findCustomerByCallId() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalCallCustomer customer = this.baseAllService.getCustomerService().findCustomerByCallId(callUnt.getId());
		map.put("customer", customer);
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 导出全部-未处理
	 * @return null
	 * @author liy
	 * @datetime 2015年1月6日,上午9:59:30
	 */
	@Action(value = "call_manage_my_unt_export_all")
	public String exportAll(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename="+FileNameEncoderUtil.encode(request, "我的未处理话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getCallMyManagerService().untExportAll(admin,this.callUnt);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前-未处理
	 * @return null
	 * @author liy
	 * @datetime 2015年1月6日,上午9:59:47
	 */
	@Action(value = "call_manage_my_unt_export_cur")
	public String exportCurrent(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename="+FileNameEncoderUtil.encode(request, "我的未处理话务任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			if(StringUtils.isNotBlank(callUnt.getId())){
				Workbook workbook = this.baseAllService.getCallMyManagerService().untExportCurrent(callUnt.getId());
				workbook.write(toClient);
			}
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 查询打开任务允许修改的状态
	 * @return
	 * @author kangyj
	 * @datetime 2015年1月16日,下午5:08:41
	 */
	@Action(value = "call_manage_my_unt_status")
	public String findCallStatusKey() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getCallMyManagerService().findCallStatusKey(admin, callUnt.getKeyType());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 打开任务修改状态
	 * @return
	 * @author kangyj
	 * @datetime 2015年1月26日,上午11:35:41
	 */
	@Action(value = "call_open_edit_status")
	public String openEditStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.baseAllService.getCallMyManagerService().editCallStatus(admin, callUnt);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前打开任务所处状态
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月8日,下午5:49:14
	 */
	@Action(value = "call_find_status")
	public String findCallStatus() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		String callStatus = this.baseAllService.getCallMyManagerService().findCallStatus(callUnt.getId());
		map.put("callStatus", callStatus);
		map.put("callId", callUnt.getId());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 合并任务，删除任务，将号码合并到将要打开的任务中
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月9日,下午12:37:33
	 */
	@Action(value = "call_merge_phone")
	public String mergePhone() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getCallMyManagerService().mergePhone(callUnt.getCallStatus(), callUnt.getId(), callUnt.getOpenCallId());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 合并任务，将当前任务客户信息合并到老任务中
	 * @return
	 * @author kangyj
	 * @datetime 2015年2月9日,下午4:21:57
	 */
	@Action(value = "call_merge_call")
	public String mergeCall() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getCallMyManagerService().mergeCall(callUnt.getCallStatus(), callUnt.getId(), callUnt.getCustomerId(), callUnt.getOpenCallId());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalCallUntreated getModel() {
		return callUnt;
	}
	

}
